package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.UserCart;
import org.csu.mypetstore.persistence.CartDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private static String GET_CART_LIST="select * from cart where userid=?";
    private static String INSERT_CART_LIST="insert into cart (itemid,productid,listprice,number,userid,decsn) values(?,?,?,?,?,?)";
    private static String UPDATE_CART_LIST="update cart set number=? where userid=? and itemid=?";
    private static String SELECT_ITEM="select * from cart where userid=? and itemid=?";
    private static String SELECT_PRODUCT="select * from item where itemid=?";
    @Override
    public List<UserCart> getCartListByUserid(String userid) {
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
        List<UserCart> userCartList=new ArrayList<UserCart>();

        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(GET_CART_LIST);
            stm.setString(1,userid);
            rs=stm.executeQuery();

            while(rs.next())
            {
                UserCart cart=new UserCart();
                cart.setItemid(rs.getString("itemid"));
                cart.setProductid(rs.getString("productid"));
                cart.setListprice(rs.getDouble("listprice"));
                cart.setUserid(rs.getString("userid"));
                cart.setNumber(rs.getInt("number"));
                cart.setDescn(rs.getString("decsn"));
                userCartList.add(cart);
            }
            DBUtil.close(conn,stm,rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userCartList;
    }

    @Override
    public void insertCartList(UserCart cart) {
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;

        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(INSERT_CART_LIST);
            stm.setString(1,cart.getItemid());
            stm.setString(2,cart.getProductid());
            stm.setDouble(3,cart.getListprice());
            stm.setInt(4,cart.getNumber());
            stm.setString(5,cart.getUserid());
            stm.setString(6,cart.getDescn());

            stm.executeUpdate();

            DBUtil.close(conn,stm,rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateCartList(UserCart cart) {
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;

        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(UPDATE_CART_LIST);
            stm.setInt(1,cart.getNumber());
            stm.setString(2,cart.getUserid());
            stm.setString(3,cart.getItemid());

            stm.executeUpdate();
            DBUtil.close(conn,stm,rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean selectCartList(UserCart cart) {
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
        boolean index=true;

        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(SELECT_ITEM);
            stm.setString(1,cart.getUserid());
            stm.setString(2,cart.getItemid());

            rs=stm.executeQuery();
            if(rs.next())//如果有，则判断为错误
            {
                index=false;
            }
            DBUtil.close(conn,stm,rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return index;

    }

    @Override
    public UserCart getUserCart(String userid) {//传参命名有错，应为itemid
        Connection conn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;

        UserCart cart=new UserCart();
        try {
            conn=DBUtil.getConnection();
            stm=conn.prepareStatement(SELECT_PRODUCT);
            stm.setString(1,userid);

            rs=stm.executeQuery();

            if(rs.next())
            {
                cart.setProductid(rs.getString("productid"));
                cart.setListprice(Double.parseDouble(rs.getString("listprice")));
                cart.setDescn(rs.getString("attr1"));
            }
            DBUtil.close(conn,stm,rs);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return cart;

    }
}
